package com.runjian.rundo.iot.agent.iec104.parser.frame.monitoring.process;

import com.runjian.rundo.iot.agent.iec104.parser.frame.ASDUFrame;
import com.runjian.rundo.iot.agent.iec104.parser.frame.Cp56Time2a;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ByteUtil;
import com.runjian.rundo.iot.agent.iec104.parser.frame.monitoring.base.BaseMonitoringFrame;
import lombok.Getter;
import lombok.Setter;

/**
 * M_ME_TF_1(36, "(遥测)短浮点数,带品质描述词,带时标CP56Time2a的浮点型测量值"),
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public class M_ME_TF_1 extends BaseMonitoringFrame {

    @Override
    public void analysisInfo(byte[] msg) {
        if (this.getSq()) {
            // 地址连续
            int sAddress = ByteUtil.bytesToInt(new byte[] {msg[12], msg[13], msg[14], 0x00});
            for (int i = 0; i < this.getNumber(); i++) {
                this.addInfoElement(InfoElement.<Float>builder().address(sAddress + i)
                    .value(Float.intBitsToFloat(ByteUtil.bytesToInt(ArrayUtil.sub(msg, 15 + i * 12, 19 + i * 12))))
                    .qds((short)(msg[19 + i * 12] & 0xff)).date(Cp56Time2a.of(ArrayUtil.sub(msg, 20 + i * 12, 27 + i * 12))).build());
            }
        } else {
            for (int i = 0; i < this.getNumber(); i++) {
                this.addInfoElement(InfoElement.<Float>builder()
                    .address(ByteUtil.bytesToInt(new byte[] {msg[12 + (15 * i)], msg[13 + (15 * i)], msg[14 + (15 * i)], 0x00}))
                    .value(Float.intBitsToFloat(ByteUtil.bytesToInt(ArrayUtil.sub(msg, 15 + i * 15, 19 + i * 15))))
                    .qds((short)(msg[19 + i * 15] & 0xff)).date(Cp56Time2a.of(ArrayUtil.sub(msg, 20 + i * 15, 27 + i * 15))).build());
            }
        }
    }

}
